package com.jimikongjian.service.dao.impl;

import com.jimikongjian.dao.IComment;
import com.jimikongjian.models.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/4.
 */
@Service("iCommentImpl")
public class ICommentImpl implements IComment {

    @Autowired
//    @Qualifier("commentMapper")
    private IComment iComment;

    /**
     * 通过评论的id号，获取某一评论信息
     *
     * @param id
     * @return List<Comment>
     */
    public List<Comment> getCommentListById(int id) {
        if (id > 0){
            return iComment.getCommentListById(id);
        }
        return null;
    }

    /**
     * 通过评论的id号，获取某一评论的信息
     *
     * @param id
     * @return Comment
     */
    public Comment getCommentById(int id) {
        if (id > 0){
            List<Comment> commentList = getCommentListById(id);
            if (commentList.size() > 0){
                return commentList.get(0);
            }
        }
        return null;
    }

    /**
     * 通过某个文章的id的评论
     *
     * @param articleId
     * @param start
     * @param length
     * @return List<Comment>
     */
    public List<Comment> getCommentListByArticleId(int articleId, int start, int length) {
        if (articleId > 0 && start >= 0 && length > 0){
            return iComment.getCommentListByArticleId(articleId, start, length);
        }
        return null;
    }

    /**
     * 通过某个文章的id和被评论者的id评论
     *
     * @param articleId
     * @param commentParentId
     * @param start
     * @param length          @return List<Comment>
     */
    @Override
    public List<Comment> getCommentListByArticleIdAndCommentParentId(int articleId, int commentParentId, int start, int length) {
        if (articleId > 0 && commentParentId > 0 && start >= 0 && length > 0){
            return iComment.getCommentListByArticleIdAndCommentParentId(articleId, commentParentId, start, length);
        }
        return null;
    }

    /**
     * 通过某个文章的id的评论
     *
     * @param articleId
     * @param commentId
     * @param start
     * @param length    @return List<Comment>
     */
    @Override
    public List<Comment> getCommentListByArticleIdAndCommentId(int articleId, int commentId, int start, int length) {
        if (articleId > 0 && commentId > 0 && start >= 0 && length > 0){
            return iComment.getCommentListByArticleIdAndCommentId(articleId, commentId, start, length);
        }
        return null;
    }

    /**
     * 插入一条新的评论信息
     *
     * @param comment
     */
    public void insertComment(Comment comment) {
        if (comment.getArticleId() > 0
                && comment.getCommentId() > 0
                && comment.getCommentNick() != ""
                && comment.getCommentParentId() >= 0
                && comment.getCommentParentUserId() >0
                && comment.getCommentParentUserNick() != ""
                && comment.getCommentedCount() >= 0
                && comment.getContent() != ""){
            iComment.insertComment(comment);
        }


    }

    /**
     * 更新一条新的评论信息
     *
     * @param comment
     */
    public void updateComment(Comment comment) {
//        不允许用户更新评论,所以该方法不向外界暴露
        if (comment.getArticleId() > 0
                && comment.getCommentId() > 0
                && comment.getCommentNick() != ""
                && comment.getCommentParentId() >= 0
                && comment.getCommentParentUserId() > 0
                && comment.getCommentParentUserNick() != ""
                && comment.getCommentedCount() >= 0
                && comment.getContent() != ""){
            iComment.updateComment(comment);
        }

    }

    /**
     * 将评论的被评论数+1
     *
     * @param comment
     */
    @Override
    public void addCommentedCountByCommentParentId(Comment comment) {
        if (comment.getCommentParentId() > 0){
            iComment.addCommentedCountByCommentParentId(comment);
        }
    }

    /**
     * 删除一条评论信息
     *
     * @param comment
     */
    public void deleteComment(Comment comment) {
//        不允许用户删除评论，所以该方法不向外界暴露
        if (comment.getId() > 0){
            iComment.deleteComment(comment);
        }

    }
}
